const crypto = require('crypto');
const nodeRSA = require('node-rsa');
// 生成一个1024长度的密钥对
const key = new nodeRSA({b: 1024});
// 导出公钥
const publicKey = key.exportKey('public');
console.log("输出公钥",publicKey)
// 导出私钥
const privateKey = key.exportKey('private');
console.log("输出私钥",privateKey)
const secret = 'hello ashin!'
// 使用私钥加密，公钥解密
const encrypt = crypto.privateEncrypt(privateKey, Buffer.from(secret));
const decrypt = crypto.publicDecrypt(publicKey, encrypt);
// 也可反过来
// const encrypt = crypto.publicDecrypt(publicKey, Buffer.from(secret));
// const decrypt = crypto.privateEncrypt(privateKey, encrypt);
console.log('加密后：', encrypt.toString('base64'));
console.log('解密后：', decrypt.toString());